package com.ruoyi.bantong.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.bantong.service.IBtOrganizationRechargeRecordService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bantong.mapper.BtOrganizationMapper;
import com.ruoyi.bantong.domain.BtOrganization;
import com.ruoyi.bantong.service.IBtOrganizationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 机构Service业务层处理
 * 
 * @author laohei
 * @date 2021-01-27
 */
@Service
public class BtOrganizationServiceImpl implements IBtOrganizationService 
{
    @Autowired
    private BtOrganizationMapper btOrganizationMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IBtOrganizationRechargeRecordService organizationRechargeRecordService;

    /**
     * 查询机构
     * 
     * @param orgId 机构ID
     * @return 机构
     */
    @Override
    public BtOrganization selectBtOrganizationById(Integer orgId)
    {
        return btOrganizationMapper.selectBtOrganizationById(orgId);
    }

    /**
     * 查询机构列表
     * 
     * @param btOrganization 机构
     * @return 机构
     */
    @Override
    public List<BtOrganization> selectBtOrganizationList(BtOrganization btOrganization)
    {
        return btOrganizationMapper.selectBtOrganizationList(btOrganization);
    }

    /**
     * 新增机构
     * 
     * @param btOrganization 机构
     * @return 结果
     */
    @Override
    public int insertBtOrganization(BtOrganization btOrganization)
    {
        btOrganization.setCreateTime(DateUtils.getNowDate());
        //邀请码
        btOrganization.setInvitationCode(SpringUtils.getCharAndNumr(6));
        addSysUser(btOrganization);
        btOrganization.setCreateTime(DateUtils.getNowDate());
        return btOrganizationMapper.insertBtOrganization(btOrganization);
    }

    /**
     * 修改机构
     * 
     * @param btOrganization 机构
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBtOrganization(BtOrganization btOrganization)
    {
        btOrganization.setUpdateTime(DateUtils.getNowDate());
        BtOrganization orgTemp = btOrganizationMapper.selectBtOrganizationById(btOrganization.getOrgId());
        if(!StringUtils.isNull(btOrganization.getPhone())){
            String phone = btOrganization.getPhone();
            SysUser usertemp = userService.selectUserByUserName(orgTemp.getPhone());
            SysUser user = new SysUser();
            user.setUserName(phone);
            user.setUserId(usertemp.getUserId());
            Long[] roleIds = {new Long(100)};
            user.setRoleIds(roleIds);
            userService.updateUser(user);
        }
        return btOrganizationMapper.updateBtOrganization(btOrganization);
    }

    /**
     * 批量删除机构
     * 
     * @param orgIds 需要删除的机构ID
     * @return 结果
     */
    @Override
    public int deleteBtOrganizationByIds(Integer[] orgIds)
    {
        return btOrganizationMapper.deleteBtOrganizationByIds(orgIds);
    }

    /**
     * 删除机构信息
     * 
     * @param orgId 机构ID
     * @return 结果
     */
    @Override
    public int deleteBtOrganizationById(Integer orgId)
    {
        return btOrganizationMapper.deleteBtOrganizationById(orgId);
    }

    @Override
    public int recharge(BtOrganization org) throws Exception{
        BigDecimal reBean = org.getReBean();
        String type = "in";
        Integer actiontype = 1;
        if(reBean.compareTo(BigDecimal.ZERO) == -1){
            BigDecimal reBeanTemp = BigDecimal.ZERO.subtract(reBean);
            BtOrganization btOrganization = btOrganizationMapper.selectBtOrganizationById(org.getOrgId());
            BigDecimal reBeanRe = btOrganization.getReBean();
            if(reBeanTemp.compareTo(reBeanRe) == 1){
                throw new CustomException("机构充值金豆不足！");
            }
            type = "out";
            actiontype = 3;
            org.setReBean(reBeanTemp);
        }
        organizationRechargeRecordService.updataOrgRecharge(org.getOrgId(),null,org.getReBean(),BigDecimal.ZERO,type,actiontype);
        return 1;
    }

    private void addSysUser(BtOrganization btOrganization){
        SysUser user = new SysUser();
        user.setDeptId(new Long(110));
        user.setNickName(btOrganization.getOrganizationName());
        user.setPhonenumber(btOrganization.getPhone());
        user.setStatus("0");
        user.setUserName(btOrganization.getPhone());
        Long[] roleIds = {new Long(100)};
        user.setRoleIds(roleIds);
        user.setCreateBy(SecurityUtils.getUsername());
        user.setPassword(SecurityUtils.encryptPassword(btOrganization.getPassword()));
        userService.insertUser(user);
    }
}
